Skip to content

Commit 4b18f70

Browse files
authored
Merge pull request #6554 from akatsoulas/optimize-queries
Fix filtering query
2 parents a0823d0 + 4c0d5c6 commit 4b18f70

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

kitsune/flagit/views.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@
3131
def get_flagged_objects(reason=None, exclude_reason=None, content_model=None, product_slug=None):
3232
"""Retrieve pending flagged objects with optional filtering, eager loading related fields."""
3333
queryset = FlaggedObject.objects.pending().select_related("content_type", "creator")
34+
3435
if exclude_reason:
3536
queryset = queryset.exclude(reason=exclude_reason)
3637
if reason:
3738
queryset = queryset.filter(reason=reason)
3839
if content_model:
3940
queryset = queryset.filter(content_type=content_model)
40-
if product_slug:
41-
matching_product_ids = [
42-
obj.id
43-
for obj in queryset
44-
if hasattr(obj.content_object, "product")
45-
and obj.content_object.product.slug == product_slug
46-
]
47-
queryset = queryset.filter(id__in=matching_product_ids)
41+
42+
if product_slug:
43+
model_class = content_model.model_class()
44+
45+
if hasattr(model_class, "product"):
46+
matching_objects = model_class.objects.filter(product__slug=product_slug)
47+
matching_ids = matching_objects.values_list("id", flat=True)
48+
49+
queryset = queryset.filter(object_id__in=matching_ids)
50+
4851
return queryset
4952

5053

0 commit comments

Comments
 (0)